﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Base;
using Silvermoon.Shapes;
using System.Drawing;
using Silvermoon.UI;

namespace Silvermoon.Controls
{
    public class BubbleButton : Control
    {
        private Shape content;
        private bool isPressed;

        public BubbleButton()
            : base()
        {
        }

        public BubbleButton(Shape content)
            : this()
        {
            Content = content;
        }

        public Shape Content
        {
            get { return content; }
            set
            {
                if (content != value)
                {
                    content = value;
                    Invalidate(InvalidationFlag.LayoutAppearance);
                }
            }
        }

        public bool IsPressed
        {
            get { return isPressed; }
            private set
            {
                if (isPressed != value)
                {
                    isPressed = value;
                    OnPressedChanged();
                }
            }
        }

        public Bubble Bubble
        {
            get { return Parent as Bubble; }
        }

        public override bool CanFocus
        {
            get { return false; }
        }

        protected internal override void OnTouchDown(TouchEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;
                IsPressed = true;
            }
            base.OnTouchDown(e);
        }

        protected internal override void OnTouchUp(TouchEventArgs e)
        {
            IsPressed = false;
            e.Handled = true;
            base.OnTouchUp(e);
        }

        protected override void OnLayout()
        {
            if (CheckFlag(InvalidationFlag.Appearance))
            {
                if (isPressed) Transformation = new ScaleTransform(0.92f, 0.92f, 1f); else Transformation = null;
                Shapes.Clear();
                if (content != null)
                {
                    Shapes.AddLast(content);
                }
            }
            if (CheckFlag(InvalidationFlag.Layout | InvalidationFlag.Size))
            {
                if (content != null)
                {
                    content.Location = new Point((Width - content.Width) / 2, (Height - Content.Height) / 2);
                    content.Size = content.Size;
                    //content.Bounds = Control.ScaledRect(0, 0, Width, Height);
                }
            }
            base.OnLayout();
        }

        protected virtual void OnPressedChanged()
        {
            SetState(isPressed ? VState.ButtonPressed : VState.Normal);
            Bubble bubble = Bubble;
            if (bubble != null) bubble.Invalidate(InvalidationFlag.Appearance);
            Invalidate(InvalidationFlag.Appearance);
        }
    }
}
